﻿namespace Probel.NDoctor.Statistics.UnitTests
{
    using System;
    using System.Collections.Generic;

    using NUnit.Framework;

    using Probel.NDoctor.Statistics.Domain;
    using Probel.NDoctor.Statistics.UnitTests.Helpers;

    [TestFixture]
    public abstract class BaseStatisticsExporter
    {
        #region Fields

        protected readonly IFactory Factory;

        private readonly DateTime Today = Data.Today;

        private IStatisticsExporter Exporter;

        #endregion Fields

        #region Constructors

        protected BaseStatisticsExporter(IFactory factory)
        {
            this.Factory = factory;
        }

        #endregion Constructors

        #region Methods

        [Test]
        public void Export_Durations_Exported()
        {
            var count = this.Factory.Scalars().GetDurationCount();
            var stats = new List<SessionDuration>();

            for (int i = 0; i < 10; i++)
            {
                stats.Add(new SessionDuration()
                {
                    Duration = new TimeSpan(1, 0, 4),
                    TimeStamp = DateTime.Today,
                    Version = "1.0.0",
                });
            }

            this.Exporter.Insert(stats);

            var scalar = this.Factory.Scalars();
            Assert.AreEqual(count + 10, scalar.GetDurationCount());
        }

        [Test]
        public void Export_OneStatistic_Exported()
        {
            var count = this.Factory.Scalars().GetStatisticsCount();
            var stats = new List<StatisticEntry>();

            for (int i = 0; i < 10; i++)
            {
                stats.Add(new StatisticEntry()
                {
                    ExecutionTime = 10,
                    Threshold = 5,
                    IsPossibleBottleneck = (i % 2 == 0),
                    MethodName = Data.RandomString,
                    TargetTypeName = Data.RandomString,
                    TimeStamp = Today,
                    Application = Data.Application,
                });
            }

            this.Exporter.Insert(stats);

            var scalar = this.Factory.Scalars();
            Assert.AreEqual(count + 10, scalar.GetStatisticsCount());
        }

        [Test]
        public void Update_Application_Updated()
        {
            var exporter = this.Factory.Exporter();
            var scalar = this.Factory.Scalars();
            var app = scalar.GetApplicationByAppKey(Data.AppKey);
            Assert.NotNull(app);

            app.ApplicationKey = app.ApplicationKey;
            app.InstallationDate = app.InstallationDate;
            app.LastUpdate = app.LastUpdate.AddDays(10);
            app.UpdateVersion = app.UpdateVersion.AddDays(10);
            app.Version = app.Version;
            exporter.Update(app);

            var toTest = scalar.GetApplicationByAppKey(Data.AppKey);

            Assert.AreEqual(app.ApplicationKey, toTest.ApplicationKey);
            Assert.AreEqual(app.InstallationDate, toTest.InstallationDate);
            Assert.AreEqual(app.LastUpdate, toTest.LastUpdate);
            Assert.AreEqual(app.UpdateVersion, toTest.UpdateVersion);
            Assert.AreEqual(app.Version, toTest.Version);
        }

        [SetUp]
        public void _Setup()
        {
            this.Exporter = this.Factory.Exporter();
            this.Factory.Exporter().ClearRepository();
            this.Factory.Import(Data.Applications, Data.Statistics, Data.Durations);
        }

        #endregion Methods
    }
}